package org.mspring.mlog.web.module.web;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.mspring.mlog.entity.Album;
import org.mspring.mlog.entity.Photo;
import org.mspring.mlog.entity.constants.PageNames;
import org.mspring.mlog.service.AlbumService;
import org.mspring.mlog.service.PhotoService;
import org.mspring.mlog.utils.PermissionUtils;
import org.mspring.platform.config.ConfigService;
import org.mspring.platform.dao.Order.OrderType;
import org.mspring.platform.dao.Page;
import org.mspring.platform.dao.query.QueryBuilder;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.freemarker.FreemarkerVariableNames;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @since 2014年5月6日
 * @author Gao Youbo
 */
@Widget
@RequestMapping("/photo")
public class Web_PhotoWidget extends Web_AbstractWidget {

    @Autowired
    private ConfigService configService;
    @Autowired
    private PhotoService photoService;
    @Autowired
    private AlbumService albumService;

    @RequestMapping("/list")
    public String list(@RequestParam(required = false) long album, @RequestParam(defaultValue = "1") int page, HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) throws IOException {
        if (album < 1) {
            response.sendError(404);
            return null;
        }
        // 判断权限
        boolean has_permission = PermissionUtils.hasAlbumPermission(album, session);
        if (!has_permission) {
            model.addAttribute("albumId", album);
            return "skin:/album-token";
        }
        Page<Photo> photoPage = new Page<>();
        if (photoPage.getOrder() == null || photoPage.getOrder().isEmpty()) {
            photoPage.addOrder("id", OrderType.DESC);
        }
        String pageSize = configService.getConfig("web_photolist_size");
        if (StringUtils.isNotBlank(pageSize)) {
            photoPage.setPageSize(Integer.parseInt(pageSize));
        }
        photoPage.setPageNo(page);
        QueryBuilder queryBuilder = QueryBuilder.instance(Photo.class).buildValuedEqual("album.id", album);
        photoService.listPage(queryBuilder, photoPage);
        Album albumObject = albumService.get(album);
        model.addAttribute(FreemarkerVariableNames.PHOTO_PAGE, photoPage);
        model.addAttribute(FreemarkerVariableNames.ALBUM, albumObject);
        setCurrnetPage(model, PageNames.PHOTO_LIST);
        return "skin:/photo-list";
    }

    @RequestMapping("/view")
    public String view(@RequestParam(required = false) long id, HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        if (id < 1) {
            response.sendError(404);
            return null;
        }
        Photo photo = photoService.get(id);
        Album album = albumService.get(photo.getAlbum().getId());
        photo.setAlbum(album);

        model.addAttribute(FreemarkerVariableNames.PHOTO, photo);
        model.addAttribute(FreemarkerVariableNames.ALBUM, album);
        setCurrnetPage(model, PageNames.PHOTO_VIEW);
        return "skin:/photo-view";
    }
}
